package com.yyy.service;

import com.yyy.dao.RecordDao;
import com.yyy.entity.Record;
import com.yyy.util.DateUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author：yyy
 * @Package：com.yyy.service
 * @Project：HuTuBiill
 * @name：ReportService
 * @Date：2024/11/11 22:19
 * @Filename：ReportService
 * 报表业务层
 */
public class ReportService {
    RecordDao dao=new RecordDao();
    /*
    * 获取一天的消费金额
    * */
    public int getDaySpend(Date date){
        int daySpend=0;
        List<Record> dayRecords=dao.selectByDate(date);
        for(Record record:dayRecords){
            daySpend+=record.spend;
        }
        return daySpend;
    }

    /*
    * 获取一个月的每天汇总消费记录
    * */
    public List<Record> listThisMonthRecords(){
        List<Record> result=new ArrayList<>();
        Date monthBegin= DateUtil.monthBegin();
        int monthTotal=DateUtil.thisMonthTotalDay();
        Calendar c=Calendar.getInstance();
        for (int i = 0; i < monthTotal; i++) {
            Record r=new Record();
            c.setTime(monthBegin);
            c.add(Calendar.DATE,i);
            Date eachDate=c.getTime();
            int daySpend=getDaySpend(eachDate);
            r.spend=daySpend;
            result.add(r);

        }
        return result;
    }

}
